package fr.afcepf.ai89.esharing.metier;

import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import fr.afcepf.ai89.esharing.data.DAOMessagePrive;
import fr.afcepf.ai89.esharing.data.IDAOMessagePrive;
import fr.afcepf.ai89.esharing.entity.MessagePrive;
import fr.afcepf.ai89.esharing.entity.Proprietaire;

public class BusinessCommuication implements IBusinessCommunication {

	@Override
	public MessagePrive ouvrirMsgPriveLu(int idMsgPrive) {
		
		MessagePrive msg = new MessagePrive();
		
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOMessagePrive proxyDataMsg = null;
			
			try {
				
				proxyDataMsg = (IDAOMessagePrive)ctx.lookup("java:global/ESharingPhase1/DAOMessagePrive!fr.afcepf.ai89.esharing.data.IDAOMessagePrive");
			
			}
			catch (Exception e ) {
				
				proxyDataMsg = (IDAOMessagePrive)ctx.lookup("ESharingPhase1/DAOMessagePrive!fr.afcepf.ai89.esharing.data.IDAOMessagePrive");
			
			}
			
			msg = proxyDataMsg.getMsgPrive(idMsgPrive);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return msg;
				
	}

	@Override
	public int envoyerMsgPrive(MessagePrive msg) {
		
		int id = 0;
		
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOMessagePrive proxyDataMsg = null;
			
			try {
				
				proxyDataMsg = (IDAOMessagePrive)ctx.lookup("java:global/ESharingPhase1/DAOMessagePrive!fr.afcepf.ai89.esharing.data.IDAOMessagePrive");
			
			}
			catch (Exception e ) {
				
				proxyDataMsg = (IDAOMessagePrive)ctx.lookup("ESharingPhase1/DAOMessagePrive!fr.afcepf.ai89.esharing.data.IDAOMessagePrive");
			
			}
			
			id = proxyDataMsg.envoyerMsgPrive(msg);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return id;
		
	}

	@Override
	public MessagePrive ouvrirMsgPriveNonLu(int idMsgPrive) {
		
		MessagePrive msg = this.ouvrirMsgPriveLu(idMsgPrive);
		
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOMessagePrive proxyDataMsg = null;
			
			try {
				
				proxyDataMsg = (IDAOMessagePrive)ctx.lookup("java:global/ESharingPhase1/DAOMessagePrive!fr.afcepf.ai89.esharing.data.IDAOMessagePrive");
			
			}
			catch (Exception e ) {
				
				proxyDataMsg = (IDAOMessagePrive)ctx.lookup("ESharingPhase1/DAOMessagePrive!fr.afcepf.ai89.esharing.data.IDAOMessagePrive");
			
			}
			
			int idMP = (int)proxyDataMsg.marquerLuMsgPrive(msg);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		msg = this.ouvrirMsgPriveLu(idMsgPrive);
		
		return msg;
		
	}

	@Override
	public List<MessagePrive> getMsgsPrivesRecus(Proprietaire des) {
		
		List<MessagePrive> mesMessages = new ArrayList<MessagePrive>();
		
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOMessagePrive proxyDataMsg = null;
			
			try {
				
				proxyDataMsg = (IDAOMessagePrive)ctx.lookup("java:global/ESharingPhase1/DAOMessagePrive!fr.afcepf.ai89.esharing.data.IDAOMessagePrive");
			
			}
			catch (Exception e ) {
				
				proxyDataMsg = (IDAOMessagePrive)ctx.lookup("ESharingPhase1/DAOMessagePrive!fr.afcepf.ai89.esharing.data.IDAOMessagePrive");
			
			}
			
			mesMessages = proxyDataMsg.getMsgsPrivesRecus(des);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return mesMessages;
		
	}

	@Override
	public List<MessagePrive> getMsgsPrivesEnvoyes(Proprietaire exp) {

		List<MessagePrive> mesMessages = new ArrayList<MessagePrive>();
		
		try {
			
			Context ctx;
			ctx = new InitialContext();
			IDAOMessagePrive proxyDataMsg = null;
			
			try {
				
				proxyDataMsg = (IDAOMessagePrive)ctx.lookup("java:global/ESharingPhase1/DAOMessagePrive!fr.afcepf.ai89.esharing.data.IDAOMessagePrive");
			
			}
			catch (Exception e ) {
				
				proxyDataMsg = (IDAOMessagePrive)ctx.lookup("ESharingPhase1/DAOMessagePrive!fr.afcepf.ai89.esharing.data.IDAOMessagePrive");
			
			}
			
			mesMessages = proxyDataMsg.getMsgsPrivesEnvoyes(exp);
				
		}
		catch (NamingException e) {
			
			e.printStackTrace();
			
		}
		
		return mesMessages;

	}

}
